package bb.lanxing.lib.devices.ble.ble.base;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import bb.lanxing.lib.devices.sprint.Command;
import bb.lanxing.lib.devices.sprint.Commands;
import bb.lanxing.lib.devices.sprint.Response;
//import bb.lanxing.lib.devices.sprint.coroutines.DeviceCoroutinesScopeeKt;
import kotlin.Metadata;
import kotlin.Result;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.functions.Function3;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import kotlinx.coroutines.BuildersKt;
import kotlinx.coroutines.CoroutineScope;
import kotlinx.coroutines.Dispatchers;
import kotlinx.coroutines.sync.Mutex;
import kotlinx.coroutines.sync.MutexKt;

public final class BaseResponsiveCharacteristic {
    private final BaseBleCharacteristic cmdCharacteristics;
    private final Mutex mutex;
    private Continuation<? super Response> responseContinuation;
    private final BaseBleCharacteristic responsiveCharacteristic;

    public BaseResponsiveCharacteristic(BaseBleCharacteristic cmdCharacteristics, BaseBleCharacteristic responsiveCharacteristic, boolean z) {
//        Intrinsics.checkNotNullParameter(cmdCharacteristics, "cmdCharacteristics");
//        Intrinsics.checkNotNullParameter(responsiveCharacteristic, "responsiveCharacteristic");
        this.cmdCharacteristics = cmdCharacteristics;
        this.responsiveCharacteristic = responsiveCharacteristic;
        this.mutex = MutexKt.Mutex(false);
        if (z) {
            responsiveCharacteristic.enableIndications();
        } else {
            responsiveCharacteristic.enableNotify();
        }
        responsiveCharacteristic.setValueUpdateCallback(new AnonymousClass1());
    }

    public /* synthetic */ BaseResponsiveCharacteristic(BaseBleCharacteristic baseBleCharacteristic, BaseBleCharacteristic baseBleCharacteristic2, boolean z, int i, DefaultConstructorMarker defaultConstructorMarker) {
        this(baseBleCharacteristic, baseBleCharacteristic2, (i & 4) == 0 && z);
    }

    public BaseBleCharacteristic getCmdCharacteristics() {
        return this.cmdCharacteristics;
    }

    public BaseBleCharacteristic getResponsiveCharacteristic() {
        return this.responsiveCharacteristic;
    }

    private final class AnonymousClass1 implements Function2<BluetoothGatt, BluetoothGattCharacteristic, Unit> {
        AnonymousClass1() {
            super();
        }

        @Override
        public Unit invoke(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            invoke2(bluetoothGatt, bluetoothGattCharacteristic);
            return Unit.INSTANCE;
        }

        public void invoke2(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            Intrinsics.checkNotNullParameter(gatt, "gatt");
            Intrinsics.checkNotNullParameter(characteristic, "characteristic");
            Response createResponse = Commands.createResponse(characteristic.getValue());
            Continuation continuation = BaseResponsiveCharacteristic.this.responseContinuation;
            if (continuation != null) {
                Result.Companion companion = Result.Companion;
//                continuation.resumeWith(Result.m2854constructorimpl(createResponse));
            }
        }
    }

    public boolean sendRaw(byte[] byteArray) {
        Intrinsics.checkNotNullParameter(byteArray, "byteArray");
        return this.cmdCharacteristics.write(byteArray);
    }

    public boolean sendCommand(Command command) {
        Intrinsics.checkNotNullParameter(command, "command");
        byte[] bytes = command.toBytes();
        Intrinsics.checkNotNullExpressionValue(bytes, "command.toBytes()");
        return sendRaw(bytes);
    }

    public static /* synthetic */ void executeRaw(BaseResponsiveCharacteristic baseResponsiveCharacteristic, byte[] bArr, Function3 function3, Function1 function1, int i, Object obj) {
        if ((i & 4) != 0) {
//            function1 = BaseResponsiveCharacteristic$executeRaw$1.INSTANCE;
        }
        baseResponsiveCharacteristic.executeRaw(bArr, function3, function1);
    }

    public void executeRaw(byte[] byteArray, Function3<? super CoroutineScope, ? super Response, ? super Continuation<? super Unit>, ? extends Object> successCallback, Function1<? super CoroutineScope, Unit> errorCallback) {
        Intrinsics.checkNotNullParameter(byteArray, "byteArray");
        Intrinsics.checkNotNullParameter(successCallback, "successCallback");
        Intrinsics.checkNotNullParameter(errorCallback, "errorCallback");
//        BuildersKt.launch(DeviceCoroutinesScopeeKt.getDeviceStateScope(this.cmdCharacteristics.getBleService().getDevice()), Dispatchers.getIO(), null, new BaseResponsiveCharacteristic$executeRaw$2(this, byteArray, successCallback, errorCallback, null), 2, null);
    }

    public static /* synthetic */ void execute(BaseResponsiveCharacteristic baseResponsiveCharacteristic, Command command, Function3 function3, Function1 function1, int i, Object obj) {
        if ((i & 4) != 0) {
//            function1 = BaseResponsiveCharacteristic$execute$1.INSTANCE;
        }
        baseResponsiveCharacteristic.execute(command, function3, function1);
    }

    public void execute(Command command, Function3<? super CoroutineScope, ? super Response, ? super Continuation<? super Unit>, ? extends Object> successCallback, Function1<? super CoroutineScope, Unit> errorCallback) {
//        Intrinsics.checkNotNullParameter(command, "command");
//        Intrinsics.checkNotNullParameter(successCallback, "successCallback");
//        Intrinsics.checkNotNullParameter(errorCallback, "errorCallback");
        byte[] bytes = command.toBytes();
//        Intrinsics.checkNotNullExpressionValue(bytes, "command.toBytes()");
        executeRaw(bytes, successCallback, errorCallback);
    }
}
